<template>
  <div class="arti-box" ref="listwrap" @scroll="onscroll">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          @click="skip(item.art_id)"
          v-for="(item, index) in list"
          :key="item.art_id"
        >
          <h4 class="title_sizi">{{ item.title }}</h4>
          <van-grid :border="false" :column-num="item.cover.type">
            <van-grid-item v-for="v in item.cover.images">
              <van-image :src="v" />
            </van-grid-item>
          </van-grid>

          <div class="span_list">
            <span>{{ item.aut_name }}</span>
            <span>{{ item.comm_count }}评论</span>
            <span>{{ item.pubdate | formatTime }}</span>
            <van-icon class="icon_x" name="cross" />
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { articleListAPI } from "@/api";
export default {
  props: ["channe_id"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      isLoading: false,
      progressBar: 0,
    };
  },
  activated() {
    this.$refs.listwrap.scrollTop = this.progressBar;
  },
  methods: {
    async onRefresh() {
      let res = await articleListAPI({
        channel_id: this.channe_id,
        timestamp: Date.now(),
      });
      this.timestamp = res.data.data.pre_timestamp;
      this.list = res.data.data.results;
      this.isLoading = false;
    },
    async onLoad() {
      let res = await articleListAPI({
        channel_id: this.channe_id,
        timestamp: this.timestamp,
      });
      this.timestamp = res.data.data.pre_timestamp;
      this.list.push(...res.data.data.results);
      this.loading = false;
      if (this.timestamp === null) {
        this.finished = true;
      }
    },
    skip(id) {
      this.$router.push("/home/article?back=" + id);
    },
    onscroll() {
      this.progressBar = this.$refs.listwrap.scrollTop;
    },
  },
};
</script>

<style scoped lang="less">
.arti-box {
  position: fixed;
  top: 90px;
  left: 0;
  bottom: 50px;
  width: 100%;
  overflow: auto;
  .title_sizi {
    font-weight: normal;
    font-size: 18px;
  }
  .span_list span {
    margin-right: 6px;
    color: #96a4c4;
  }
  .icon_x {
    color: #96a4c4;
    font-size: 14px;
    float: right;
    margin-top: 4px;
  }
}
</style>